package org.framework.core.error.support;

import java.util.Locale;
import java.util.Properties;

import org.framework.core.Configation;
import org.framework.core.error.LanguageErrcode;


public class Errors1 implements LanguageErrcode {
	
	public Locale defaultLanguage;	//默认语言
	{
		String[] language = Configation.getSysProp("sys.defaultLanguage").split("_");
		if(language != null && language.length > 1){
			defaultLanguage = new Locale(language[0],language[1]);
		}else{
			defaultLanguage = Locale.getDefault();
		}
	}
	private final int result;		//一级错误码
	private final int errcode;		//二级错误码
	private final String msg;		//错误描述
	
	public Errors1(int result, int errcode) {
		this.result = result;
		this.errcode = errcode;
		this.msg = matchMsg("", defaultLanguage);
	}
	
	public Errors1(int result, int errcode, String msg) {
		this.result = result;
		this.errcode = errcode;
		this.msg = msg;
	}
	
	public void init(){
		Properties p = Configation.getProp(defaultLanguage.toString());
		if(p!=null){
			for (Object key : p.keySet()) {
				p.getProperty(key.toString());
			}
		}
	}
	
	public int getResult() {
		return this.result;
	}
	public int getErrcode() {
		return this.errcode;
	}
	public String getMsg() {
		return this.msg;
	}
	public String getMsg(Locale locale){
		return matchMsg("", locale);
	}
	
	public LanguageErrcode LOCALE(final Locale locale){
		return new LanguageErrcode() {
			
			public int getResult() {
				return result;
			}
			
			public String getMsg() {
				return matchMsg("", locale);
			}
			
			public int getErrcode() {
				return errcode;
			}

			public LanguageErrcode LOCALE(Locale locale) {
				return Errors1.this.LOCALE(locale);
			}
		};
	}
	
	private static String matchMsg(String key, String language){
		Properties p = Configation.getProp(language);
		if(p==null)
			return null;
		
		String msg = p.getProperty(key);
		
		return msg==null?("Error:["+key+"]"):msg;
	}
	
	public static String matchMsg(String key, Locale locale){
		return matchMsg(key, locale.toString());
	}
}
